
class SettingParser
{

private:

	int mi_process_a_param(char * param1, int state)
	{
		if (state == 0)
		{
			if (strcmp(param1, "-g") == 0 || strcmp(param1, "geometry") == 0)
			{
				state = 1;
			}
			if (strcmp(param1, "-t") == 0 || strcmp(param1, "port") == 0)
			{
				state = 2;
			}
			if (strcmp(param1, "-a") == 0 || strcmp(param1, "bpp") == 0)
			{
				state = 3;
			}
			if (strcmp(param1, "-u") == 0 || strcmp(param1, "username") == 0)
			{
				state = 5;
			}
			if (strcmp(param1, "-p") == 0 || strcmp(param1, "password") == 0)
			{
				state = 6;
			}
			if (strcmp(param1, "-d") == 0 || strcmp(param1, "domain") == 0)
			{
				state = 7;
			}
			if (strcmp(param1, "-s") == 0 || strcmp(param1, "shell") == 0)
			{
				state = 8;
			}
			if (strcmp(param1, "-c") == 0 || strcmp(param1, "directory") == 0)
			{
				state = 9;
			}
			if (strcmp(param1, "-n") == 0 || strcmp(param1, "hostname") == 0)
			{
				state = 10;
			}
		}
		else
		{
			if (state == 2) /* -t */
			{
				state = 0;
				strcpy_s(g_tcp_port_rdp, 10, param1);
			}
			if (state == 3) /* -a */
			{
				state = 0;
				strcpy_s(g_server_depth, 10, param1);
			}
			if (state == 5) /* -u */
			{
				state = 0;
				strcpy_s(g_username, 256, param1);
			}
			if (state == 6) /* -p */
			{
				state = 0;
				strcpy_s(g_password, 256, param1);
			}
			if (state == 7) /* -d */
			{
				state = 0;
				strcpy_s(g_domain, 256, param1);
			}
			if (state == 8) /* -s */
			{
				state = 0;
				strcpy_s(g_shell, 256, param1);
			}
			if (state == 9) /* -c */
			{
				state = 0;
				strcpy_s(g_directory, 256, param1);
			}
			if (state == 10) /* -n */
			{
				state = 0;
				strcpy_s(g_hostname, 256, param1);
			}
		}
		return state;
	}

public:

	char g_tcp_port_rdp[10];
	char g_server_depth[10];
	char g_username[256];
	char g_password[256];
	char g_domain[256];
	char g_shell[256];
	char g_directory[256];
	char g_hostname[256];
	char g_servername[256];

	bool ParseFile(HANDLE hFile)
	{
		char param[256];
		char param1[256];

		int i;
		int i1;
		int state;

		strcpy_s(g_hostname, 256, "test");
		strcpy_s(g_username, 256, "pda");

		/* defaults */
		strcpy_s(g_servername, 256, "127.0.0.1");

		/* process parameters */
		i1 = 0;
		state = 0;

		char lpCmdLine[256];

		DWORD dwBytesRead = 0;

		while(ReadFile(hFile, lpCmdLine, 256, &dwBytesRead, NULL))
		{
			if(dwBytesRead == 0)
				break;

			for (i = 0; i < dwBytesRead; i++)
			{
				if (lpCmdLine[i] != 32 && lpCmdLine[i] != 9) /* space or tab */
				{
					param[i1] = lpCmdLine[i];
					i1++;
				}
				else
				{
					param[i1] = 0;
					i1 = 0;
					strcpy_s(param1, 256, param);
					state = mi_process_a_param(param1, state);
					strcpy_s(g_servername, 256, param1);
				}
			}
		}

		if (i1 > 0)
		{
			param[i1] = 0;
			strcpy_s(param1, 256, param);
			state = mi_process_a_param(param1, state);
			strcpy_s(g_servername, 256, param1);
		}

		return (state == 0);
	}

	bool ParseSettings(LPTSTR lpCmdLine)
	{
		char param[256];
		char param1[256];

		int len;
		int i;
		int i1;
		int state;

		strcpy_s(g_hostname, 256, "test");
		strcpy_s(g_username, 256, "pda");

		/* defaults */
		strcpy_s(g_servername, 256, "127.0.0.1");

		/* process parameters */
		i1 = 0;
		state = 0;
		len = lstrlen(lpCmdLine);

		if (len == 0)
		{
			return false;
		}

		for (i = 0; i < len; i++)
		{
			if (lpCmdLine[i] != 32 && lpCmdLine[i] != 9) /* space or tab */
			{
				param[i1] = (char)lpCmdLine[i];
				i1++;
			}
			else
			{
				param[i1] = 0;
				i1 = 0;
				strcpy_s(param1, 256, param);
				state = mi_process_a_param(param1, state);
				strcpy_s(g_servername, 256, param1);
			}
		}

		if (i1 > 0)
		{
			param[i1] = 0;
			strcpy_s(param1, 256, param);
			state = mi_process_a_param(param1, state);
			strcpy_s(g_servername, 256, param1);
		}

		return (state == 0);
	}
};